<?php
  try {
    $xml = new SimpleXMLElement(@file_get_contents($_REQUEST['xsl']));
  }
  catch (Exception $e) {
    echo 'There was a problem accessing the XSL template.';
    exit;
  }
  $xslt = new XSLTProcessor();
  $xslt->importStylesheet($xml);
  try {
    $xml = new SimpleXMLElement(file_get_contents(wget(
       $_REQUEST['url']
      ,str_replace('___ID___',$_REQUEST['id'],file_get_contents($_REQUEST['xml'])) // 'xml/suraISOQuery.xml'))
    )));
  }
  catch (Exception $e) {
    echo 'There was a problem accessing the ISO record.';
    echo ' '.$e->getMessage();
    exit;
  }

  echo $xslt->transformToXml($xml);

  function wget($u,$p) {
    $request  = '/tmp/'.rand().time().'.post.xml';
    $response = '/tmp/'.rand().time().'.xml';
    file_put_contents($request,$p);
    `wget --header "Content-Type:text/xml" --post-file=$request '$u' -O $response.2 > /dev/null 2>&1`;
    // gi-cat wraps the response in csw:GetRecordByIdResponse -- get rid of it if necessary
    if (preg_match("/^2/",`grep csw:GetRecordByIdResponse $response.2 | wc -l`)) {
      $cmd = "sed -e '1d' -e '\$d' $response.2 > $response";
      `$cmd`;
      return $response;
    }
    else {
      return $response.".2";
    }
  }
?>
